package main

import (
	"fmt"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/data/binding"
	"fyne.io/fyne/v2/widget"
	"github.com/flopp/go-findfont"
	"github.com/goki/freetype/truetype"
	"math/rand"
	"os"
	"strconv"
	"time"

	"fyne.io/fyne/v2/app"
)

func main() {
	myApp := app.New()
	myWindow := myApp.NewWindow("凯局专用工具")
	input := widget.NewEntry() // 输入值
	input.Text = time.Now().Format("2006-01-02 15:04:05")
	minIncre := widget.NewEntry() // 最小增量
	minIncre.Text = "180"
	maxIncre := widget.NewEntry() // 最大增量
	maxIncre.Text = "300"
	countNumer := widget.NewEntry() // 生成个数
	countNumer.Text = "10"
	autoIncre := widget.NewCheck("", func(b bool) {
		fmt.Println(b)
	})
	show_value := binding.NewString()

	form := &widget.Form{
		Items: []*widget.FormItem{ // we can specify items in the constructor
			{Text: "输入时间", Widget: input, HintText: "请输入时间，格式如下:2020-01-01 00:00:00"},
			{Text: "最小增量", Widget: minIncre, HintText: "请输入整数，单位秒"},
			{Text: "最大增量", Widget: maxIncre, HintText: "请输入整数，单位秒"},
			{Text: "生成个数", Widget: countNumer, HintText: "请输入个数"},
			{Text: "自动递增", Widget: autoIncre},
		},
		OnSubmit: func() { // optional, handle form submission
			times, _ := time.Parse("2006-01-02 15:04:05", input.Text)
			timeUnix := times.UTC()
			minTime := timeUnix.Format("2006-01-02 15:04:05")
			minIncre_, _ := strconv.Atoi(minIncre.Text)
			maxIncre_, _ := strconv.Atoi(maxIncre.Text)
			count, _ := strconv.Atoi(countNumer.Text)
			incre_ := maxIncre_ - minIncre_
			rand.Seed(time.Now().UnixNano())
			allTime := ""
			randomTime := timeUnix
			if autoIncre.Checked == true {
				for i := 0; i < count; i++ {
					rn := rand.Intn(incre_)
					randomTime = randomTime.Add(time.Second * time.Duration(rn+minIncre_))
					allTime += randomTime.Format("2006-01-02 15:04:05") + "\n"
				}
			} else {
				for i := 0; i < count; i++ {
					rn := rand.Intn(incre_)
					randomTime_ := randomTime.Add(time.Second * time.Duration(rn+minIncre_))
					allTime += randomTime_.Format("2006-01-02 15:04:05") + "\n"
				}
			}

			s := fmt.Sprintf("%+v\n%s\n", minTime, allTime)
			show_value.Set(s)
		},
	}

	text := widget.NewEntryWithData(show_value)
	myWindow.SetContent(container.NewBorder(form, nil, nil, nil, text))
	myWindow.Resize(fyne.NewSize(200, 500))
	myWindow.Show()
	myApp.Run()
	tidyUp()
}

func tidyUp() {
	fmt.Println("Exited")
}

func init() {
	fontPath, err := findfont.Find("SIMYOU.TTF")
	if err != nil {
		panic(err)
	}
	fmt.Printf("Found 'arial.ttf' in '%s'n", fontPath)

	// load the font with the freetype library
	// 原作者使用的ioutil.ReadFile已经弃用
	fontData, err := os.ReadFile(fontPath)
	if err != nil {
		panic(err)
	}
	_, err = truetype.Parse(fontData)
	if err != nil {
		panic(err)
	}
	os.Setenv("FYNE_FONT", fontPath)
}
